const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});

//==============1最新动态==========
//业务逻辑
module.exports = function() {
    var router = express.Router();
    //查询最新动态
    router.get('/ertiaryGoods.html',function(req,res,next){
        //判断是否登陆
        if(req.session['userId']==null||req.session['userId']=="")
        {
            res.status(400).send('<script>alert("请登录");window.location.href="login.html";</script>').end();
        }
        //获得url中的参数
        var page=urlobj.parse(req.url,true).query.page;
        if(!page)
        {
            page=1;
        }
        var pageNum=10;
        //计算总页数
        db.query('SELECT *FROM `classc`ORDER BY `classc`.`classCTime` DESC',function(err,data){
            if(err)
            {

                res.status(400).send('<script>alert("计算总页数失败!");window.location.href="404.html";</script>').end();
            }
            else
            {
                if(data.length==0)
                {
                    res.status(400).send('<script>alert("当前没有数据!");window.location.href="404.html";</script>').end();
                }
                else
                {
                    var conts=data.length;
                    conts=Math.ceil(conts/pageNum);
                    //存总页数
                    req.classAId=conts;
                    //存当前页码
                    req.page=page;

                }
            }
        });


        db.query('SELECT * FROM classa, classb, classc WHERE classa.classAId = classb.classAId AND classb.classBId = classc.classBId ORDER BY `classc`.`classCTime` DESC LIMIT '+pageNum*(page-1)+' , '+pageNum+'',function(err,data){
            if(err)
            {
                res.status(500).send('<script>alert("出错了!");window.location.href="404.html";</script>').end();
            }
            else
            {
                if(data.length==0)
                {
                    res.status(400).send('<script>alert("没有数据!");window.location.reload();</script>').end();
                }
                else
                {
                    req.classcont=data;
                }
                next();
            }
        });
    });
    //删除一级分类
    router.get('/ertiaryGoods.html',function(req,res,next){
        //删除一级分类信息
        var delelt= + req.query.classAId;

        //格式化url栏的参数，转换为对像
        console.log(urlobj.parse(req.url,true).query.act);
        //用户确定要删除，查询当前对应的是否有商品,没有商品就删除成功
       if(req.query.classCId==null||req.query.classCId=="")
       {
           next();
       }else {
           if (urlobj.parse(req.url, true).query.act == "delete") {

                   db.query("DELETE FROM `classc` WHERE classCId="+ req.query.classCId+"", function (err,data) {
                       if (err) {
                           res.status(500).send('<script>alert("删除出错了!");window.location.href="404.html";</script>').end()
                       } else {
                           res.status(200).send('<script>alert("删除成功！");window.location.href="ertiaryGoods.html";</script>').end()
                       }
                   });
           }

       }

    });

   //开始渲染后台首页
    router.get('/ertiaryGoods.html',function(req,res,next){
        //res.render("admin/PrimaryClassification.ejs",{classa:req.classAName,page:req.classAId});
        res.render("admin/ertiaryGoods.ejs",{classclist:req.classcont,page:req.classAId});
    });

    return router;
}
